<template>
  <basic-container>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <avue-crud :data="scheduleData"
                 :option="option"
                 :page="page"
                 :table-loading="tableLoading"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 class="avuecrud">
        <template slot="menuRight">
          <el-button type="warning" size="small" @click="exportToDoReviewData()">导出</el-button>
        </template>
        <template slot-scope="scope"
                  slot="menu">
          <div class="menu-title"
               @click.stop="transact(scope.row,scope.index)">
            <i class="iconfont iconview icon-primary"></i>
            <p class="menu-title-text">办理</p>
          </div>
          <div class="menu-title" @click.stop="assign(scope.row,scope.index)">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">转办</p>
          </div>
        </template>
      </avue-crud>
      <el-dialog title="流程图"
                 :visible.sync="showPicDialog"
                 width="1200px">
        <img :src="actPicUrl"
             width="100%">
      </el-dialog>
      <assign :visible="assignVisible" :task-id="taskIdAssign" @cancelAssign="cancelAssign"></assign>
    </div>
  </basic-container>
</template>
<script>
import { toDoReviewList, recordReview, offlineReview,exportToDoReviewData,exportToDoReviewDataExcel} from "@/api/review";
import { REVIEW_DIC, PROJECT_DIC } from "@/const/dict";

export default {
  name: "toDoReview",
  data () {
    return {
      headerData: {
        title: '审核审批'
      },
      actPicUrl: '',
      showPicDialog: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      tableLoading: false,
      scheduleData: [],
      option: {
        page: true,
        addBtn: false,
        editBtn: false,
        addRowBtn: false,
        indexLabel: "序号",
        index: true,
        indexWidth:70,
        cellBtn: false,
        delBtn: false,
        columnBtn: false,
        refreshBtn: false,
        filterBtn: false,
        searchBtn: false,
        menuWidth: 150,
        keyId: "id",
        column: [
          {
            label: "规划名称",
            prop: "achievementTitle",
            search: true,
            cell: true
          }, {
            label: "规划类型",
            prop: "category",
            width: '120',
            dicData: PROJECT_DIC.CATEGORY,
            cell: true,
            formatter: function (row, value, label) {
              let categoryStr = '';
              let planningRankStr = '';
              switch (row.category) {
                case 'A':categoryStr='发展规划';break;
                case 'B':categoryStr='专项规划';break;
                case 'C':categoryStr='区域规划';break;
                case 'D':categoryStr='空间规划';break;
              }

              switch (row.planningRank) {
                case 'A':planningRankStr='重点';break;
                case 'B':planningRankStr='一般';break;
                case 'C':planningRankStr='备案';break;
              }
              return planningRankStr+categoryStr
            }
          },
          {
            label: "拟发布范围",
            prop: "openScope",
            type: "select",
            width: '120',
            dicData: REVIEW_DIC.publishDIC,
            cell: true
          },
          {
            label: "环节",
            prop: "state",
            type: "select",
            width: '100',
            // dicData: REVIEW_DIC.stateDIC,
            formatter: function (row, value, label) {
              switch (row.state) {
                case '0':
                  return "暂存";
                case '1':
                  return "修改";
                case '2':
                  if (row.planningRank == 'B' && row.category == 'B') {
                    return "审批";
                  } else {
                    return "审核";
                  }
                case '3':
                  if (row.category == 'A') {
                    return "审核";
                  } else {
                    return "审批";
                  }
                case '4':
                  return "审批";
                case '5':
                  return "市级政府审批";
                case '6':
                  return "市级人大审议";
                case '7':
                  return "省级政府审批";
                case '8':
                  return "省级人大审议";
                case '9':
                  return "报备";
                case '10':
                  return "结束";
                case '11':
                  return "线下流程";
              }
            },
            cell: true,
          }
        ]
      },
      assignVisible: false,
      taskIdAssign: null,
      titleLikeSearch:''
    };
  },
  created () {
    this.getToDoList(this.page)
  },
  methods: {
    getToDoList (page, params) {
      this.tableLoading = true;
      toDoReviewList(Object.assign({
        current: page.currentPage,
        size: page.pageSize,
        reviewStateNe: '11'
      }, params)).then(response => {
        this.scheduleData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
        this.$emit("getTotal", response.data.data.total)
      })
    },
    /**
     * 刷新回调
     */
    searchReset () {
      this.titleLikeSearch = '';
      let params = {
        titleLike:this.titleLikeSearch,
      };
      this.getToDoList(this.page,params);
    },
    //搜索回调
    searchChange (params) {
      this.page.currentPage = 1
      this.getToDoList(this.page, this.dealSearchParam(params));
    },
    // 处理搜索过滤
    dealSearchParam (params) {
      if (params.achievementTitle) {
        params.titleLike = params.achievementTitle;
      }
      this.titleLikeSearch = params.achievementTitle;
      return params;
    },
    currentChange(currentPage) {
      var page = this.page;
      page.currentPage = currentPage;
      let params = {
        titleLike:this.titleLikeSearch,
      };
      this.getToDoList(page,params);
    },
    sizeChange(pageSize) {
      var page = this.page;
      page.pageSize = pageSize;
      let params = {
        titleLike:this.titleLikeSearch,
      };
      this.getToDoList(page,params);
    },
    // 办理
    transact (row, index) {
      let state = parseInt(row.state);
      if (state in [0, 1]) {
        let src = `/approvalRelease/detail/${row.achievementId}`;
        this.$router.push({
          path: src,
          query: {
            reviewId: row.reviewId,
          }
        })
      }
      if (state > 1 && state < 9) {
        let src = `/transact/type/${row.state}`;
        this.$router.push({
          path: src,
          query: {
            achievementId: row.achievementId,
            taskId: row.taskId,
            reviewId: row.reviewId,
            state: row.state,
            category: row.category,
            planningRank: row.planningRank,
            achievementTitle: row.achievementTitle,
          }
        })
      }
      // 报备
      if (state == 9) {
        let src = `/reviewDetail/detail/${row.reviewId}`;
        this.$router.push({
          path: src,
          query: {
            taskId: row.taskId,
            stateFlag: 'record'
          }
        })
      }
      // 线下流程
      if (state == 11) {
        this.offlineReview(row.taskId)
      }
    },
    // 线下流程确认
    offlineReview (taskId) {
      this.$confirm("确定线下流程确认么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        offlineReview(taskId).then(response => {
          if (response.data.code == 0) {
            this.$notify({
              title: '成功',
              message: '线下流程确认成功',
              type: 'success',
              duration: 2000
            });
            this.$router.go(-1);
          }
        })
      })
    },
    handleSubmit () {
    },
    // 详情
    viewDetail (row, index) {
      let src = `/reviewDetail/detail/${row.reviewId}`;
      this.$router.push({
        path: src
      })
    },
    // 流程图
    flowChart (row, index) {
      this.actPicUrl =
        `/planning/registerinfo/task/view/` +
        row.taskId;
      this.showPicDialog = true;
    },
    assign(row) {
      this.taskIdAssign = row.taskId;
      this.assignVisible = true;
    },
    cancelAssign() {
      this.assignVisible = false;
    },
    exportToDoReviewData(){
      this.$confirm("确定要导出审核审批列表吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        exportToDoReviewDataExcel(Object.assign({
          reviewStateNe: '11'
        })).then(response => {
          let exportFileName = "审核审批列表.xlsx";
          this.download(response.data, exportFileName)
        });
      })
    },
    // 下载文件
    download(data, filename) {
      if (data == null) {

      } else {
        let url = window.URL.createObjectURL(new Blob([data]));
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', filename);
        document.body.appendChild(link);
        link.click()
      }
    },


  }
};
</script>

<style lang="scss" scoped>
/deep/ .avue-crud__menu {
  min-height: 0px;
}
</style>
